取得集合中的第一筆文件資料
集合.find_one()
collection=db.website
data=collection.find_one()
print(data)
文件資料是一個Object物件必須從bson.objectid 封包載入
集合.find.one(文件編號)
from bson.objectid impory ObjectId
collection=db.website
data=collection.find_one(
ObjectId("62ed0915ac9a77aa247a283b")
)
print(data)
取得文件資料後運字典的操作方式完成
文件資料["欄位名稱"]
collection=db.website
data=collection.find_one()
print(data["_id"])
print(data["name"])
取得集合中的所有資料
集合.find()
collection=db.website
cursor=collection.find()
print(cusor) #Cursor物件
#使用for迴圈逐一取得文件
for doc in sursor:
print(doc)
請先登入後台雲端
我們先抓一筆資料看看,並查看一下他的內容
import pymongo
# 連線到MongoDB
client = pymongo.MongoClient(
"mongodb+srv://root:0000@cluster0.goald.mongodb.net/?retryWrites=true&w=majority")
db = client.website # 選擇操作 website 資料庫
Collection = db.members # 選擇操作members 集合
result = Collection.find_one()
print(result)
print(result[]"_id")
print(result["email"])
print(result["gender"])
print("success~!")
再來是取得特定資料
import pymongo
from bson.objectid import ObjectId
# 連線到MongoDB
client = pymongo.MongoClient(
"mongodb+srv://root:0000@cluster0.goald.mongodb.net/?retryWrites=true&w=majority")
db = client.website # 選擇操作 website 資料庫
Collection = db.members # 選擇操作members 集合
result = Collection.find_one()
print(result)
print(result["email"])
print(result["gender"])
# 指定文件
data = Collection.find_one(
ObjectId("62ed0915ac9a77aa247a283c")
)
print(data)
print("success~!")
我們利用新增一個可以讀寫第一筆資料的cursor物件,並用迴圈讀取後面的的資料
import pymongo
from bson.objectid import ObjectId
# 連線到MongoDB
client = pymongo.MongoClient(
"mongodb+srv://root:0000@cluster0.goald.mongodb.net/?retryWrites=true&w=majority")
db = client.website # 選擇操作 website 資料庫
Collection = db.members # 選擇操作members 集合
cursor = Collection.find()
print(cursor)
for doc in cursor:
print(doc)
print("success~!")
如果我們再doc變數後增加引索值會看到什麼結果哪?
因為最後一筆沒有name所以會出現錯誤
# Flask網站前後端互動 09 - 超連結與圖片
# 載入Flask、Request、render_template
# from gc import collect
# from typing import Collection
# from flask import Flask, request, render_template
# from flask import session
import pymongo
from bson.objectid import ObjectId
# # 建立 Application 物件,設定靜態檔案的路徑處理
# # http://127.0.0.1:5000/head.png 為圖片路徑
# app = Flask(__name__, static_folder="public", static_url_path="/")
# app.secret_key = "chichi" # 密鑰
# # 處理路徑 / 的對應函市
# @app.route("/")
# def index():
# return render_template("index.html")
# @app.route("/page")
# def page():
# name = request.args.get("nameis")
# return render_template("page.html", namepage=name)
# # 使用 GET 方法處理路徑 /hello
# @app.route("/hello")
# def hello():
# name = request.args.get("name", "")
# session["username"] = name # session["欄位名稱] = 資料
# return "你好 "+name
# @app.route("/talk")
# def talk():
# name = session["username"]
# return "你好,我們第二次見面了呢~~ "+name+" ♡"
# 連線到MongoDB
client = pymongo.MongoClient(
"mongodb+srv://root:0000@cluster0.goald.mongodb.net/?retryWrites=true&w=majority")
db = client.website # 選擇操作 website 資料庫
Collection = db.members # 選擇操作members 集合
# 把資料新增到集合中且資料為JSON的格式
# result = Collection.insert_many([{
# "name": "chichi",
# "gender": "男生",
# "email": "jddj@gmail.com",
# "level": "1"
# }, {
# "name": "chiiich",
# "gender": "女生",
# "email": "jddj@yahoo.com",
# "level": "50"
# }, {
# "可以有好幾筆資料": "對阿~~"
# }])
# result = Collection.find_one()
# print(result)
# print(result["_id"])
# print(result["email"])
# print(result["gender"])
# # 指定文件
# data = Collection.find_one(
# ObjectId("62ed0915ac9a77aa247a283c")
# )
# print(data)
cursor = Collection.find()
print(cursor)
for doc in cursor:
print(doc["name"])
print("success~!")
# 啟動Server
# app.run()